Ext.onReady(function() {
    var permission = new Array();
    Ext.Ajax.request({
        url:  '../../user/getUserMenuPermission.action',
        failure: function(result, request) {
            var responseArray = Ext.util.JSON.decode(result.responseText);
            Ext.Msg.show({
                title: '错误',
                msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + responseArray.msg + '</span>',
                buttons: Ext.MessageBox.OK,
                icon: Ext.MessageBox.WARNING
            });
        },
        success: function(result, request) {
            var responseArray = Ext.util.JSON.decode(result.responseText);
            permission[0] = responseArray.permission1;   //录入
            permission[1] = responseArray.permission2;   //修改
            permission[2] = responseArray.permission3;   //删除
            permission[3] = responseArray.permission4;   //审核
            permission[4] = responseArray.permission5;   //预留1
            permission[5] = responseArray.permission6;   //预留2
            //            delButton.setDisabled(permission[2] != '1');
            delButton2.setDisabled(permission[2] != '1');
        },
        params: { menuId: currentMenuId}
    });
    Ext.SSL_SECURE_URL = "../../ext-3.2.1/resources/images/default/s.gif";
    Ext.BLANK_IMAGE_URL = "../../ext-3.2.1/resources/images/default/s.gif";
    //    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
    Ext.util.CSS.swapStyleSheet('theme', currentTheme);
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    var pageSize = 20;
    var logTypestore = new Ext.data.SimpleStore({
        fields: ['value', 'label'],
        data : [
            ['', '全部日志'],
            ['1', '安全日志'],
            ['2', '应用日志']
        ]
    });
    new Ext.Viewport({
        region:'center',
        layout:'fit',
        items:grid
    });

    //    var delButton = new Ext.Toolbar.Button({
    //        icon    : "../../ext/examples/shared/icons/fam/delete.gif",
    //        id      : 'delButton',
    //        cls: 'x-btn-text-icon',
    //        text    : '删除',
    //        disabled:true,
    //        tooltip : '删除已选中的记录。',
    //        handler : onDelClick
    //    });
    var delButton2 = new Ext.Toolbar.Button({
        icon    : "../../ext-3.2.1/examples/shared/icons/fam/del.png",
        id      : 'delButton2',
        cls: 'x-btn-text-icon',
        text    : '清除日志',
        disabled:true,
        tooltip : '删除系统日志，只保留最近90天的日志。',
        handler : onDelClick2
    });
    function onDelClick2(item) {
        Ext.Msg.confirm('删除记录', '警告：此操作将会删除三个月以前所有日志！', function(btn) {
            if (btn == 'yes') {
                Ext.MessageBox.wait('正在处理，请稍等...');
                Ext.Ajax.request({
                    url:  '../../systemLog/deleteAllKeepSomeDay.action?days=90',
                    failure: function(result, request) {
                        Ext.MessageBox.hide();
                        Ext.Msg.show({
                            title: '错误',
                            msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '删除失败！' + '</span>',
                            buttons: Ext.MessageBox.OK,
                            icon: Ext.MessageBox.WARNING
                        });
                    },
                    success: function(result, request) {
                        Ext.MessageBox.hide();
                        Ext.Msg.show({
                            title: '成功',
                            msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '删除成功！' + '</span>',
                            buttons: Ext.MessageBox.OK,
                            icon: Ext.MessageBox.INFO
                        });
                        gridStore.reload();
                    }
                });
            }
        });

    }

    //    function onDelClick(item) {
    //        var selectedCount = sm.getCount();
    //        if (selectedCount == 0) {
    //            Ext.Msg.alert('删除操作', '未选中要删除的数据，请选择要删除的数据！');
    //            return;
    //        } else {
    //            Ext.Msg.confirm('删除记录', '请确认要删除记录吗?按是将删除选中记录！', function(btn) {
    //                if (btn == 'yes') {
    //                    var records = sm.getSelections();
    //                    var _ids = '';
    //                    for (var i = 0; i < records.length; i++) {
    //                        _ids += records[i].id;
    //                        _ids += '_';
    //                    }
    //                    Ext.MessageBox.wait('正在处理，请稍等...');
    //                    Ext.Ajax.request({
    //                        url:  '../../systemLog/delete.action',
    //                        failure: function(result, request) {
    //                            Ext.MessageBox.hide();
    //                            Ext.Msg.show({
    //                                title: '错误',
    //                                msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '删除失败！' + '</span>',
    //                                buttons: Ext.MessageBox.OK,
    //                                icon: Ext.MessageBox.WARNING
    //                            });
    //                        },
    //                        success: function(result, request) {
    //                            Ext.MessageBox.hide();
    //                            Ext.Msg.show({
    //                                title: '成功',
    //                                msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '删除成功！' + '</span>',
    //                                buttons: Ext.MessageBox.OK,
    //                                icon: Ext.MessageBox.INFO
    //                            });
    //                            gridStore.reload();
    //                        },
    //                        params: { deleteID: _ids}
    //                    });
    //                }
    //            });
    //        }
    //    }

    var gridStore = new Ext.data.Store({
        proxy: new Ext.data.ScriptTagProxy({
            url: '../../systemLog/buildJSONString.action'
        }),
        reader: new Ext.data.JsonReader({
            root: 'results',
            totalProperty: 'total',
            id: 'logId'
        }, [
            {name: 'districtCode', mapping: 'districtCode'},
            {name: 'districtName', mapping: 'districtName'},
            {name: 'orgId', mapping: 'orgId'},
            {name: 'orgName', mapping: 'orgName'},
            {name: 'deptId', mapping: 'deptId'},
            {name: 'deptName', mapping: 'deptName'},
            {name: 'perId', mapping: 'perId'},
            {name: 'perName', mapping: 'perName'},
            {name: 'remoteAddr', mapping: 'remoteAddr'},
            {name: 'operDate', mapping: 'operDate'},
            {name: 'operContent', mapping: 'operContent'},
            {name: 'logType', mapping: 'logType'}
        ]),
        remoteSort: true
    });

    var sm = new Ext.grid.CheckboxSelectionModel();
   // var cm = new Ext.grid.LockingColumnModel([//sm,
    var cm = new Ext.ux.grid.LockingColumnModel([
        {
        id: 'logId',
        header: "主键",
        dataIndex: 'logId',
        width: 90,
        sortable: false,
        hidden:true,
        css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
    },
        {
            id: 'districtName',
            header: "地区",
            dataIndex: 'districtName',
            width: 80,
            align:'center',
            sortable: true,
            hidden:false,
            css: 'white-space:normal;border:#eee solid;border-width:0 1 0 1;'
        },   {
            id: 'orgName',
            header: "单位",
            dataIndex: 'orgName',
            width:230,
            align:'center',
            sortable: true,
            css: 'textAlign:"left";white-space:normal;border:#eee solid;border-width:0 1 0 1;'
        },
        {
            id: 'deptName',
            header: "科室",
            dataIndex: 'deptName',
            align:'center',
            width: 120,
            hidden:true,
            sortable: true,
            css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
        },   {
            id: 'perName',
            header: "人员",
            dataIndex: 'perName',
            align:'center',
            width:80,
            hidden:false,
            sortable: true,
            css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
        }, {
            id: 'remoteAddr',
            header: "来访地址",
            dataIndex: 'remoteAddr',
            align:'center',
            width:100,
            hidden:false,
            sortable: true,
            css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
        }, {
            id: 'operDate',
            header: "访问时间",
            dataIndex: 'operDate',
            align:'center',
            width:120,
            hidden:false,
            sortable: true,
            css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
        },{
            id: 'operContent',
            header: "日志内容",
            dataIndex: 'operContent',
            align:'center',
            width:120,
            hidden:false,
            sortable: false,
            css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
        }, {
            id: 'logType',
            header: "日志类型",
            dataIndex: 'logType',
            align:'center',
            width:80,
            hidden:true,
            sortable: false,
            renderer:function (value) {
                var bv = '';
                for (var i = 0; i < logTypestore.getCount(); i++) {
                    if (logTypestore.getAt(i).data['value'] == value) {
                        bv = logTypestore.getAt(i).data['label'];
                        break;
                    }
                }
                return  bv;
            },
            css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
        }
    ]);



    var logTxt = new Ext.form.TextField({
        id:'logTxt',
        name: 'logTxt',
        width:120
    });
    var perName = new Ext.form.TextField({
        id:'perName',
        name: 'perName',
        width:100
    });
    var startingDate = new Ext.form.DateField({
        fieldLabel: '起始日期',
        id:'startingDateString',
        name: 'startingDateString',
        width:100,
        format : "Y-m-d"
    });
    var endDate = new Ext.form.DateField({
        fieldLabel: '结束日期',
        id:'endDateString',
        name: 'endDateString',
        width:100,
        format : "Y-m-d"
    });
    var queryButton = new Ext.Toolbar.Button({
        icon    : "../../ext-3.2.1/examples/shared/icons/fam/cog.png",
        id      : 'queryButton',
        cls: 'x-btn-text-icon',
        text    : '查询',
        disabled:false,
        tooltip : '根据输入的名称查询系统日志。',
        handler : onQueryClick
    });
    function onQueryClick(item) {
        if ((startingDate.getValue() != '') && (endDate.getValue() != '')) {
            gridStore.proxy = new Ext.data.ScriptTagProxy({
                url:encodeURI('../../systemLog/buildJSONString.action?operContent=' + logTxt.getValue() + '&perName=' + perName.getValue() + '&operDate1=' + startingDate.getValue().dateFormat('Y-m-d') + '&operDate2=' + endDate.getValue().dateFormat('Y-m-d'))
            });
        } else {
              gridStore.proxy = new Ext.data.ScriptTagProxy({
                url:encodeURI('../../systemLog/buildJSONString.action?operContent=' + logTxt.getValue() + '&perName=' + perName.getValue())
            });
        }

        gridStore.reload();
    }
    // grid = new Ext.grid.LockingGridPanel({
    var grid = new Ext.ux.grid.SSNJLOCKPAGEGrid({
       // el:'grid-div',
        height:gridHeight,
        id:'grid',
        layout:'fit',
        store:gridStore ,
        loadMask: {msg:'正在加载数据，请稍候……'},
        cm:cm,
//        sm: sm,
        viewConfig: {
            enableRowBody:false
            //            forceFit:true
        },
        bodyStyle:'width:100%;',
        tbar:[delButton2,'-','&nbsp;&nbsp;&nbsp;&nbsp;','姓名：',perName,'日期：',startingDate,'至',endDate,'日志内容：',logTxt,queryButton,'-'],
        frame:false,
        stripeRows:true
        //bbar:pagingToolbar
    });

    grid.render(Ext.get('grid-div'));
    gridStore.load({params:{start:0, limit:pageSize}});




})